<template>
  <div>
    <el-skeleton style="height: 50vh" :rows="4" :loading="loading" animated>
      <el-table
        :height="400"
        :data="tableData"
        style="width: 100%"
        v-loading="loading"
        :header-cell-style="{
          background: '#F7F9FD',
          color: '#000',
          height: '48px',
          borderTop: '1px soild #E4E8F2'
        }"
      >
        <template #empty>
          <el-empty :image-size="150" description="暂无数据"></el-empty>
        </template>

        <el-table-column type="index" label="序号" width="80">
          <template v-slot="scope">
            <span>{{
              (pageState.pageNum - 1) * pageState.pageSize + scope.$index + 1
            }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="witnessName" label="单据名称"></el-table-column>
        <el-table-column prop="beginTime" label="验收时间"> </el-table-column>
        <el-table-column
          prop="partybHandlerName"
          label="提交人"
        ></el-table-column>
        <el-table-column label="状态" width="80">
          <template #default="{ row }">
            <span v-if="row.approveStatus == 1">待验收</span>
            <span v-if="row.approveStatus == 2">已完成</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="80">
          <template #default="{ row }">
            <el-button type="text" size="small" @click="handleLook(row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <re-pagination
        v-if="total > 0"
        :total="total"
        v-model:page="pageState.pageNum"
        v-model:limit="pageState.pageSize"
        @pagination="handleChangePage"
      />
    </el-skeleton>
  </div>
</template>
<script lang="ts">
import RePagination from "/@/components/RePagination/index.vue";
import { defineComponent, ref, toRef } from "vue";
import { witnessRecordList } from "/@/api/construction";
import { handlePreview } from "/@/utils/file";
export default defineComponent({
  components: {
    RePagination
  },
  name: "checkList",
  props: {
    checkItem: {
      type: Object,
      default: () => {}
    }
  },
  emits: ["closeList"],
  setup(props, { emit }) {
    const tableData = ref([]);
    const total = ref<number>(0);
    const loading = ref<boolean>(false);
    const checkItemProp = toRef(props, "checkItem");
    const pageState = {
      pageNum: 1,
      pageSize: 10,
      checkItemId: checkItemProp.value?.id
    };
    //获取检查列表
    const getRecordList = async () => {
      try {
        loading.value = true;
        const { code, data } = await witnessRecordList(pageState);
        if (code === 200) {
          tableData.value = data.list;
          total.value = data.rows;
          if (tableData.value?.length) {
            tableData.value.forEach(item => {
              if (item.partybAccAttachmentArr?.length) {
                item.partybAccAttachmentArr = onInitFileList(
                  item.partybAccAttachmentArr
                );
              }
            });
          }
        }
      } catch (err) {
        console.log(err);
      } finally {
        loading.value = false;
      }
    };
    const handleLook = async row => {
      const instanceId = row.instanceId;
      emit("closeList", instanceId);
    };

    const handleChangePage = () => {
      getRecordList();
    };

    const onInitFileList = (list: any[]) => {
      if (!list || !list.length || !list[0]?.fileName) return;
      const newFileList = list.map(item => {
        return {
          name: item?.fileName,
          response: {
            data: item,
            code: 200
          }
        };
      });
      return newFileList;
    };

    getRecordList();
    return {
      tableData,
      total,
      loading,
      pageState,
      handleLook,
      getRecordList,
      handleChangePage,
      handlePreview
    };
  }
});
</script>
<style lang="scss" scoped>
:deep .el-dialog__body {
  padding: 16px;
}

:deep .el-table {
  border: none;
}

:deep .el-table__fixed-right::before {
  display: none;
}
</style>
